<?php 
	jimport('joomla.application.component.model');
	class sale_tsc_orderModelorder_sp extends JModel
	{
        //cap nhat lai stt
        function cap_nhat_stt_sale_order($id_bill)
        {
            $db = &JFactory::getDBO();
            $sql = "SELECT *
                    FROM #__erp_sale_order od
                    WHERE id_order = '".$id_bill."'
                ";
            $db->setQuery($sql);
            //print_r($db->loadObjectList());die;
            $data = $db->loadObjectList();   
            foreach($data as $row)
            {
                $sql = "UPDATE #__erp_sale_order SET
                                status = 2
                                WHERE 
                                id ='".$row->id."'";
                                $db->setQuery($sql);
                                $db->query();
            }
        }	   
        //cap nhat so luong
        function cap_nhat_lai_so_luong($id_pro,$id_wh,$num)
        {
            
            $db = &JFactory::getDBO();
            $sql = "select * from #__erp_warehouse_ex_pro 
            where 
            id_product = '".$id_pro."' 
            and id_warehouse = '".$id_wh."'
            and number_of > 0
            ORDER BY date_in
            ";
			$db->setQuery($sql);
            $data = $db->loadObjectList();	
            foreach($data as $row)
            {
                $num = $row->number_of+$num;
                    $sql = "UPDATE #__erp_warehouse_ex_pro SET
                                number_of = '".$num."'
                                WHERE 
                                id ='".$row->id."'";
                                $db->setQuery($sql);
                                $db->query();
                    return;
            }    
        }	   
        
        //danh sach sp theo bill
        function ds_bill($id_bill)
        {
            $db = &JFactory::getDBO();
            $sql = "SELECT ep.name as pro_name, od.number, od.status, ep.id as id_pro
                    FROM #__erp_sale_order od, #__erp_product ep
                    WHERE id_order = '".$id_bill."'
                    AND ep.id = od.id_product
                ";
            $db->setQuery($sql);
            //print_r($db->loadObjectList());die;
            return $db->loadObjectList();    
        }
        
        //xoa buzy
        function xoa_buz($id)
        {
            $db = &JFactory::getDBO();
            $sql = "select id_order from #__erp_sale where id = '".$id."'";
            $db->setQuery($sql);
            $buz = $db->loadResult();
            $sql = "DELETE FROM #__erp_sale_order_buzy
                    WHERE id_order ='".$buz."'";
                                $db->setQuery($sql);
                                $db->query();        
        }
        
	    //day danh sach sp de tru so luong order
        function giam_sl_theo_order($id_pro,$id_wh,$num)
        {
            $db = &JFactory::getDBO();
            $sql = "select * from #__erp_warehouse_ex_pro 
            where 
            id_product = '".$id_pro."' 
            and id_warehouse = '".$id_wh."'
            ORDER BY date_in
            ";
			$db->setQuery($sql);
            $data = $db->loadObjectList();	
            //echo $num;
            //print_r($data);die;
            foreach($data as $row)
            {
                if($row->number_of >= $num)
                {
                    $num = $row->number_of-$num;
                    $sql = "UPDATE #__erp_warehouse_ex_pro SET
                                number_of = '".$num."'
                                WHERE 
                                id ='".$row->id."'";
                                $db->setQuery($sql);
                                $db->query();
                    return;
                }
                else
                {
                    $num = $num - $row->number_of;
                    $sql = "UPDATE #__erp_warehouse_ex_pro SET
                                number_of = '".$num."'
                                WHERE 
                                id ='".$row->id."'";
                                $db->setQuery($sql);
                                $db->query();
                }
            }
        }
        
        //lay busy
        function lay_buz()
        {
       	    $db = &JFactory::getDBO();
            $sql = "select id_order from #__erp_sale_order_buzy";
			$db->setQuery($sql);
            //print_r($db);
            //echo "<pre>";print_r($db->loadObjectList());echo "</pre>";die;
			return $db->loadObjectList();    
        }
        
        //danh sach san pham co trong kho	   
		function danh_sach_san_pham()
        {
            $con =  $_SESSION['config'];
            //echo $con->id_warehouse;die;
            //lay danh sach san pham trong kho thuoc chi nhanh thong qua id_a_b
                    $sql = "select ep.id as id_pro,ep.id_product,ep.name, ep.img, ept.id as id_type, ept.name as name_type, sum(eabw.number_of) as number_of, ep.price, cur.name as cur_name, cur.id as cur_id, cur.symbol, ui.name as ui_name
                            from #__erp_product ep ,#__erp_product_type ept ,#__erp_warehouse_ex_pro eabw ,  #__erp_financial_currency cur, #__erp_product_unit ui
                            where 
                            ep.id_type_product = ept.id and 
                            ep.id = eabw.id_product and
                            ep.id_currency = cur.id and
                            ept.type = 0 and
                            ui.id = ep.id_unit and
                            eabw.id_warehouse = '".$con->id_warehouse."'
                            GROUP BY eabw.id_product
                            order by ept.name  
                           
                        ";
                        //echo $sql;
                        
			$db = &JFactory::getDBO();
			$db->setQuery($sql);
            //print_r($db);
            //echo "<pre>";print_r($db->loadObjectList());echo "</pre>";die;
			return $db->loadObjectList();	
        }
        

	}
?>